package com.venutech.gongan.api;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.venutech.gongan.dto.rpc.common.ApiBusinessCode;
import com.venutech.gongan.entity.Jkgw;
import com.venutech.gongan.entity.QRCode;
import com.venutech.gongan.service.JkgwService;
import com.venutech.gongan.service.QRCodeService;
import com.venutech.gongan.service.WorkService;
import com.venutech.gongan.util.GpsUtil;
import com.venutech.gongan.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


@RestController
@Api(tags = {"工单巡检打卡API"})
public class WorkApiController {
    @Resource
    private WorkService workService;
    @Autowired
    private QRCodeService qrCodeService;
    @Autowired
    private JkgwService jkgwService;

    @RequestMapping(value = "/api/workSheet",method = RequestMethod.POST)
    @ApiOperation(value = "提交运维工单(内网)")
    @ResponseBody
    public ApiResponse workSheet(@RequestBody WorkSheetDto workSheetDto) {
        WorkDto workDto = new WorkDto();
        workDto.setUserId(workSheetDto.getUserId());
        workDto.setType(workSheetDto.getType());
        workDto.setContent(workSheetDto.getContent());
        workDto.setPoleId(workSheetDto.getPoleId());
        workDto.setEquipmentId(workSheetDto.getEquipmentId());
        if (Strings.isNotBlank(workSheetDto.getPicId1())) {
            workDto.setWk_ppciture_1(workSheetDto.getPicId1());
        }
        if (Strings.isNotBlank(workSheetDto.getPicId2())) {
            workDto.setWk_ppciture_2(workSheetDto.getPicId2());
        }
        if (Strings.isNotBlank(workSheetDto.getPicId3())) {
            workDto.setWk_ppciture_3(workSheetDto.getPicId3());
        }
        //插入数据库
        int i = workService.insertWorkDto(workDto);
        if (i>0){
            return ApiResponse.success(workDto);
        }else {
            return ApiResponse.error(ApiBusinessCode.CLIENT_ERROR.getCode(),
                    ApiBusinessCode.CLIENT_ERROR.getMsg()+"提交运维工单错误！");
        }

    }


    @RequestMapping(value = "/api/external/workSheet",method = RequestMethod.POST)
    @ApiOperation(value = "提交运维工单-外网")
    @ResponseBody
    public ApiResponse workExternalSheet(@RequestBody WorkSheetDto2 workSheetDto) {

        WorkExtDto workExtDto = new WorkExtDto();
        workExtDto.setWk_sq_open_id(workSheetDto.getOpenId());
        workExtDto.setType(workSheetDto.getType());
        workExtDto.setContent(workSheetDto.getContent());
        workExtDto.setPoleId(workSheetDto.getPoleId());

        if (Strings.isNotBlank(workSheetDto.getPicId1())) {
            workExtDto.setWk_ppciture_1(workSheetDto.getPicId1());
        }
        //插入数据库
        int i = workService.insertWorkExtDto(workExtDto);
        if (i>0){
            return ApiResponse.success(workExtDto);
        }else {
            return ApiResponse.error(ApiBusinessCode.CLIENT_ERROR.getCode(),
                    ApiBusinessCode.CLIENT_ERROR.getMsg()+"外网提交运维工单错误！");
        }
    }

    @RequestMapping(value = "/api/patrolRecord", method = RequestMethod.POST)
    @ApiOperation(value = "巡检打卡-内网")
    @ResponseBody
    public ApiResponse patrolRecord(@RequestBody PatrolDto patrolDto) {
        //根据qrId查询出杆位经纬度信息
        QRCode qrCode = qrCodeService.getByIdMD5(patrolDto.getQrId());
        int qrId = qrCode.getQr_id();
        Jkgw jkgw = jkgwService.getByLatAndLng(qrId);
        WorkDto workDto = new WorkDto();
        workDto.setUserId(patrolDto.getUserId());
        workDto.setPoleId(jkgw.getGw_id());
        workDto.setType(4);
        workDto.setStartTime(new Date().toString());
        workDto.setEndTime(new Date().toString());
        boolean bool = GpsUtil.getDistance(Double.valueOf(patrolDto.getLongitude()), Double.valueOf(patrolDto.getLatitude()), Double.valueOf(jkgw.getGw_jd()), Double.valueOf(jkgw.getGw_wd())) <= 0.01;
        if (bool) {
            //打卡成功
            if (workService.poleWorkDto(workDto) > 0) {
                return ApiResponse.success();
            }else{
                return ApiResponse.error(ApiBusinessCode.CLIENT_ERROR.getCode(),
                        ApiBusinessCode.CLIENT_ERROR.getMsg() + "巡检打卡失败:");
            }
        } else {
            return ApiResponse.error(ApiBusinessCode.CLIENT_ERROR.getCode(),
                    ApiBusinessCode.CLIENT_ERROR.getMsg() + "巡检打卡失败:不在打卡范围内1");
        }
    }

    @RequestMapping(value = "/api/pole/approveWork", method = RequestMethod.GET)
    @ApiOperation(value = "完成工单-内网")
    public ApiResponse approveWork(Integer workId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        workService.approveWork(workId, loginUser.getUserId().intValue());
        return ApiResponse.success();
    }
}
